﻿Imports System
Imports System.IO
Imports System.Collections
Imports System.Diagnostics

Public Class Cls_File_Search
    Private Function IsCharString(ByVal inkey As Char, ByVal compare As String) As Boolean
        Dim u As String = compare.ToUpper
        Dim l As String = compare.ToLower
        Return ((inkey = Char.Parse(u)) OrElse (inkey = Char.Parse(l)))
    End Function

    Private Function IsArg(ByVal arg As String, ByVal switchtext As String) As Boolean
        Return ((switchtext.Substring(1).ToLower.Trim = arg.ToLower))
    End Function

    Private Bulunan_Dosyalar As New DataTable
    Public Function Dosya_Bul(ByVal file_name As String, ByVal ilkbul As Boolean) As DataTable
        Dim maindir As String = GetCurrentDrive()
        Dim initdirs As ArrayList = New ArrayList
        initdirs.Add(maindir)
        Bulunan_Dosyalar.Columns.Add("YOL", GetType(String))
        While initdirs.Count > 0
            If FileFound(initdirs, file_name, ilkbul) Then
                If ilkbul = True Then
                    Exit While
                End If
            End If
        End While
        Return Bulunan_Dosyalar
    End Function
    Private Function FileFound(ByRef dirs As ArrayList, ByVal target As String, ByVal ilkbul As Boolean) As Boolean
        Dim subdirs As ArrayList = New ArrayList
        Dim foundfiles As ArrayList
        Dim ienum As IEnumerator = dirs.GetEnumerator
        While ienum.MoveNext
            If FileInFolder(target, CType(ienum.Current, String), foundfiles) Then
                DumpFileNames(foundfiles)
                If ilkbul = True Then
                    Exit While
                End If
            End If
            subdirs.InsertRange(0, GetSubDirs(CType(ienum.Current, String)))
        End While
        dirs = subdirs
        Return False
    End Function

    Private Sub DumpFileNames(ByVal files As ArrayList)
        Dim ienum As IEnumerator = files.GetEnumerator
        While ienum.MoveNext
            Bulunan_Dosyalar.Rows.Add(CType(ienum.Current, String))
        End While
    End Sub

    Private Function FileInFolder(ByVal file As String, ByVal dir As String, ByRef answer As ArrayList) As Boolean
        answer = New ArrayList
        Try
            Dim files As String() = Directory.GetFiles(dir, file)
            Dim i As Integer = 0
            While i < files.Length
                answer.Add(files(i))
                System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
            End While
            Return (files.Length > 0)
        Catch generatedExceptionVariable0 As System.UnauthorizedAccessException
            Return False
        End Try
    End Function

    Private Function GetSubDirs(ByVal dir As String) As ArrayList
        Dim retval As ArrayList = New ArrayList
        Try
            Dim subdirs As String() = Directory.GetDirectories(dir, "*")
            Dim i As Integer = 0
            Dim j As Integer = subdirs.Length
            While i < j
                retval.Add(subdirs(i))
                System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
            End While
        Catch generatedExceptionVariable0 As System.UnauthorizedAccessException
        End Try
        Return retval
    End Function

    Private Function GetCurrentDrive() As String
        Dim curdir As String = Directory.GetCurrentDirectory
        If curdir.Length > 3 Then
            curdir = curdir.Remove(3, curdir.Length - 3)
        End If
        Return curdir
    End Function
End Class
